var code = {
    /*页面内容html首次加载*/
    list_page_load: function (type, cpn)
    {
        var list = $('#list');
        $('#loading').show();
        list.empty();
        list.load('/code/get/postlist/' + type + '/' + cpn,
            function ()
            {
                $('#loading').hide();
            });
    },
    /*显示loading*/
    showloading: function ()
    {
        $('#loading').show();
    },
    /*隐藏loading*/
    dismiss: function ()
    {
        $('#loading').hide();
    },
    /*初始化分页::当前页数::总页数*/
    init_page: function (cpn, paginalNumber)
    {
        var page_list = $('#page-list');
        page_list.empty();
        page_list.append('<li><a href="#">&laquo;</a></li>');

        var i = 1;

        if (cpn > 3 && cpn < paginalNumber - 3)
            i = cpn - 2;
        else if (cpn > paginalNumber - 3)
        {
            i = cpn - 2 - (cpn - paginalNumber + 2);
            if (i < 1)
                i = 1;
        }

        var times = 0;
        for (i; i <= paginalNumber; i++)
        {
            times++;
            if (times > 5)
                break;

            if (i == cpn)
                page_list.append('<li class="active"><a href="#">' + i + '</a></li>');
            else
                page_list.append('<li><a href="#">' + i + '</a></li>');
        }
        page_list.append('<li><a href="#">&raquo;</a></li>');
    },
    /*初始化cookies，并且加载默认列表*/
    init_cookies: function ()
    {
        var page = $.cookie('post-list-code-page');
        var type = $.cookie('post-list-code-type');

        if (type == null)
        {
            $.cookie('post-list-code-type', 'ALL');
            type = 'ALL';
        }

        if (page == null)
        {
            $.cookie('post-list-code-page', 1);
            page = 1;
        }

        code.list_page_load(type, page);

        var tag = $.cookie('post-list-code-tag');
        if (tag != null)
            $('#codetype-list').find('li:eq(' + tag + ') a').tab('show');
    },
    /*代码类型选择，写入cookie，保证刷新后依旧访问该类型*/
    select_type: function ()
    {
        $('#codetype-list').find('a').click(function ()
        {
            var ss = $(this).text();
            var type = ss.split('(')[0].trim();

            //点击表头，默认回首页，page=1
            var page = 1;
            $.cookie('post-list-code-page', page);
            type = type.replace('/', '-');//防止/在url出错，改变成-，然后在后台改变回来
            $.cookie('post-list-code-type', type);
            code.list_page_load(type, page);
        });
    },
    /*分页选择，写入cookie，保证刷新后依旧访问该页*/
    select_page: function (paginalNumber)
    {
        $('#page-list').find('li').find('a').click(function ()
        {
            var page = $(this).text();

            if (page == '&laquo;' || page == '«')
            {
                page = $.cookie('post-list-code-page');
                if (page - 1 > 0)
                    page--;
                else
                    return;
            }
            else if (page == '&raquo;' || page == '»')
            {
                page = $.cookie('post-list-code-page');
                if (page + 1 <= paginalNumber)
                    page++;
                else
                    return;
            }

            $.cookie('post-list-code-page', page);
            var type = $.cookie('post-list-code-type');
            code.list_page_load(type, page);
        });
    },
    /*代码类型选择，记录标签的位置，保证刷新后依旧显示该标签*/
    select_tag: function ()
    {
        $('a[data-toggle="tab"]').on('shown.bs.tab', function (e)
        {
            var target = e.target.toString();
            var ss = target.split("/");
            var tag = ss[ss.length - 1] - 1;
            /*Other的标签id值为100，-1后等于99，当等于99的时候，获取li的长度，赋值给tag*/
            if (tag == 99)
                tag = $('#codetype-list').find('li').length - 1;
            $.cookie('post-list-code-tag', tag);
        });
    }
};